
ARCH                 ?= arm
DEMO_BOARD           ?= ALPHA
APP_DEMO_DIR         := app_demo
DRIVERS_DEMO_DIR     := drivers_demo
DEVICE_TREE_DIR      := device_tree

# 编译工具和编译选项
ifeq ($(ARCH), arm)
	ifeq ($(DEMO_BOARD), OTHER)
      KERNELDIR            ?= ~/sdk/xxx/linux-kernel
      CROSS_COMPILE_PREFIX ?= arm-ca9-linux-uclibcgnueabihf-
      ROOT_PERMISSION      :=
      export ROOT_PERMISSION
	else ifeq ($(DEMO_BOARD), ALPHA)
      KERNELDIR            ?= ~/7Linux/imx6ull-kernel
      CROSS_COMPILE_PREFIX ?= arm-linux-gnueabihf-
	endif
else
  KERNELDIR            ?= /lib/modules/$(shell uname -r)/build
  CROSS_COMPILE_PREFIX ?=
endif

# 这里不要用V，当使用V的时候，编译驱动的时候的详细信息也会打印出来
# A=1时会打印make进出子目录调用子makefile的一些信息
ifeq ("$(origin A)", "command line")
  KBUILD_VERBOSE = $(A)
endif
ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE = 0
endif

ifneq ($(KBUILD_VERBOSE),1)
  # 这个参数可以屏蔽进入子目录录和出子目录的打印,会自动导出，所有子makefile都生效
  MAKEFLAGS += --no-print-directory
endif

# 导出的变量若是没有赋值，也会被传递到子makefile，并且值为空
export ARCH CROSS_COMPILE_PREFIX KERNELDIR

# 编译模块和测试程序
# ---------------------------------------------------------------------------
ko_test:app_demo.out drivers_demo_ko
	@echo "\033[1;35m""********** compile ko_test finish! **********""\033[0m"

all: app_demo.out drivers_demo_ko dt-dtb
	@echo "\033[1;35m""********** compile all finish! **********""\033[0m"

app_demo.out:
	@echo "\033[1;35m""********** compile app_demo ... **********""\033[0m"
	$(MAKE) -C $(APP_DEMO_DIR)

drivers_demo_ko:
	@echo "\033[1;35m""********** compile drivers_demo ... **********""\033[0m"
	$(MAKE) -C $(DRIVERS_DEMO_DIR)

dt-dtb:
	@echo "\033[1;35m""********** compile device_tree ... **********""\033[0m"
	$(MAKE) dt-dtb -C $(DEVICE_TREE_DIR)

# 安装相关资源
# ---------------------------------------------------------------------------
install: install-app install-drivers

install-app:
ifeq ($(DEMO_BOARD), OTHER)
else
	$(MAKE) install -C $(APP_DEMO_DIR)
endif

install-drivers:
ifeq ($(DEMO_BOARD), OTHER)
else
	$(MAKE) install -C $(DRIVERS_DEMO_DIR)
endif

install-dtb:
ifeq ($(DEMO_BOARD), OTHER)
else
	$(MAKE) install -C $(DEVICE_TREE_DIR)
endif
# 删除相关资源的安装
# ---------------------------------------------------------------------------
uninstall: uninstall-app uninstall-drivers

uninstall-app:
	$(MAKE) uninstall -C $(APP_DEMO_DIR)

uninstall-drivers:
	$(MAKE) uninstall -C $(DRIVERS_DEMO_DIR)


# 清理工程和调试
# ---------------------------------------------------------------------------
PHONY += FORCE
FORCE:

PHONY += clean clean-app clean-drivers clean-dtb
clean: clean-app clean-drivers
	@echo "\033[1;35m""********** make clean finish! **********""\033[0m"

distclean:clean-app clean-drivers clean-dtb
	@echo "\033[1;35m""********** make distclean finish! **********""\033[0m"

clean-app:
	@echo "\033[1;35m""********** make clean app_demo ... **********""\033[0m"
	$(MAKE) clean -C $(APP_DEMO_DIR)

clean-drivers:
	@echo "\033[1;35m""********** make clean drivers ... **********""\033[0m"
	$(MAKE) clean -C $(DRIVERS_DEMO_DIR)

clean-dtb:
	@echo "\033[1;35m""********** make clean device_tree ... **********""\033[0m"
	$(MAKE) clean -C $(DEVICE_TREE_DIR)

.PHONY: $(PHONY)

# 帮助信息
# ---------------------------------------------------------------------------
help:
	@:

print_app_demo_info:
	$(MAKE) print -C $(APP_DEMO_DIR)

print_drivers_demo_info:
	$(MAKE) print -C $(DRIVERS_DEMO_DIR)
